<?php


namespace app\controller;

use think\facade\Db;
use think\facade\Request;
use think\facade\Filesystem;

class UserController
{
    public function __construct()
    {
        // 设置CORS头
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: Content-Type, Authorization');
        header('Access-Control-Allow-Credentials: true'); // 允许发送凭据
    }

    public function getUser()
    {
        session_start(); // 开启会话

        if (!isset($_SESSION['user'])) {
            return json(['message' => '未登录'], 401);
        }

        $userID = $_SESSION['user']['userID']; // 从会话中获取用户ID
        $user = Db::name('cdydpt_user')->where('userID', $userID)->find();

        // 将图片数据编码为 Base64
        if (!empty($user['userimage'])) {
            $user['userimage'] = base64_encode($user['userimage']);
        }

        return json($user);
    }

    public function updateUser()
    {
        session_start(); // 开启会话

        if (!isset($_SESSION['user'])) {
            return json(['message' => '未登录'], 401);
        }

        $data = Request::post();
        $userID = $_SESSION['user']['userID']; // 从会话中获取用户ID

        // 更新其他信息
        $updateData = [];
        if (isset($data['username'])) {
            $updateData['username'] = $data['username'];
        }
        if (isset($data['userPhone'])) {
            $updateData['userPhone'] = $data['userPhone'];
        }
        if (isset($data['useremail'])) {
            $updateData['useremail'] = $data['useremail'];
        }

        if (!empty($updateData)) {
            Db::name('cdydpt_user')->where('userID', $userID)->update($updateData);
        }

        return json(['message' => '用户信息更新成功']);
    }

    public function updateUserImage()
    {
        session_start(); // 开启会话

        if (!isset($_SESSION['user'])) {
            return json(['message' => '未登录'], 401);
        }

        $file = Request::file('file'); // 获取上传的文件

        if (!$file) {
            return json(['message' => '没有文件上传'], 400);
        }

        $userID = $_SESSION['user']['userID']; // 从会话中获取用户ID

        try {
            // 将文件内容读取为二进制数据
            $fileContent = file_get_contents($file->getRealPath());

            // 更新数据库中的头像数据
            Db::name('cdydpt_user')->where('userID', $userID)->update(['userimage' => $fileContent]);

            return json(['message' => '头像更新成功']);
        } catch (\Exception $e) {
            return json(['message' => '服务器内部错误', 'error' => $e->getMessage()], 500);
        }
    }

    public function updatePassword()
    {
        session_start(); // 开启会话

        if (!isset($_SESSION['user'])) {
            return json(['message' => '未登录'], 401);
        }

        $data = Request::post();

        if (!isset($data['oldPassword']) || !isset($data['newPassword'])) {
            return json(['message' => '请求数据不完整'], 400);
        }

        $userID = $_SESSION['user']['userID']; // 从会话中获取用户ID
        $user = Db::name('cdydpt_user')->where('userID', $userID)->find();

        if (!$user) {
            return json(['message' => '用户不存在'], 404);
        }

        if (!password_verify($data['oldPassword'], $user['password'])) {
            return json(['message' => '旧密码不正确'], 400);
        }

        // 更新密码
        $newPasswordHash = password_hash($data['newPassword'], PASSWORD_DEFAULT);
        Db::name('cdydpt_user')->where('userID', $userID)->update(['password' => $newPasswordHash]);

        return json(['message' => '密码更新成功']);
    }

    public function deleteUser()
    {
        session_start(); // 开启会话

        try {
            $data = Request::post();
            // 记录接收到的数据
            error_log(print_r($data, true));
            // 检查学号是否存在
            $user = Db::name('cdydpt_user')->where('userID', $data['userID'])->delete();
        } catch (\Exception $e) {
            // 记录异常信息
            error_log($e->getMessage());
            return json(['code' => 1, 'msg' => '内部服务器错误']); // 返回服务器错误信息
        }
    }
}

